//
//  MyThrowingBallButton.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/17/13.
//
//

#include "MyThrowingBallButton.h"
#include "MyUtilities.h"
#include "GameLayerManager.h"

bool MyThrowingBallButton::init(){
    MySpriteButton::init();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    isFlying = false;
    velocityX = winSize.width/(360);
    return true;
}

CCTouch* MyThrowingBallButton::getTouchThatHit(cocos2d::CCSet *touches){

    CCSetIterator it = touches->begin();
    for(int i=0;i<touches->count();i++){
        CCTouch* touch = (CCTouch*)(*it);
        CCPoint touchPosition = MyUtilities::getCocosPointFromTouch(touch);
        if(isTouchedAt(touchPosition)){
            return touch;
            break;
        }
        it++;
        
    }
    return NULL;
}

void MyThrowingBallButton::handleUpdate(float dt){
    if(isFlying){
        double distanceX = endPoint.x - beginPoint.x;
        double distanceY = endPoint.y - beginPoint.y;
        double factor = 0.2f;
        double velX = distanceX*factor;//velocityX *MyUtilities::triTuyetDoi((distanceX/distanceY))* MyUtilities::layDau(distanceX);
        double velY = distanceY*factor;//velocityX*MyUtilities::triTuyetDoi((distanceY/distanceX))* MyUtilities::layDau(distanceY);
        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        setPosition(ccp(getPositionX()+velX,getPositionY()+velY));
        CCRect winRect = CCRectMake(0, 0, winSize.width, winSize.height);
        if(winRect.intersectsRect(getBoundingRect())==false){
            goBackToOriginalPosition();
        }
        MyButton* movingBall = GameLayerManager::sharedInstance()->throwingBallGameLayer->movingBall;
        if(MyUtilities::circleIntersectCircle(getBoundingRect(), movingBall->getBoundingRect())){
            goBackToOriginalPosition();
            GameLayerManager::sharedInstance()->throwingBallGameLayer->scorePoint();
        }
    }
}

void MyThrowingBallButton::goBackToOriginalPosition(){
    isFlying=false;
    setPosition(originalPosition);
}

bool MyThrowingBallButton::isTouchedAt(cocos2d::CCPoint touchPosition){
    CCRect checkRect=  CCRectMake(getPositionX()-offsetAround/2, 0, buttonSize.width+offsetAround, getPositionY()+buttonSize.height);

    return checkRect.containsPoint(touchPosition);
}

void MyThrowingBallButton::handleMultiTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if(isFlying){
        return;
    }
    if(MyButton::isTouchedAt(touches)){
        beginTouch = getTouchThatHit(touches);
        beginPoint = MyUtilities::getCocosPointFromTouch(beginTouch);
        isFocused = true;
    }
    //updateState();
}

void MyThrowingBallButton::handleMultiTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if(touches->containsObject(beginTouch)==false){
        return;
    }
    if(isFlying){
        return;
    }
    CCPoint touchPosition = MyUtilities::getCocosPointFromTouch(beginTouch);
    double distance = MyUtilities::distanceOfTwoPoints(touchPosition, beginPoint);
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    if(distance>=winSize.width*0.1f){
        isFlying = true;
        endPoint = touchPosition;
        
    }
}

void MyThrowingBallButton::handleMultiTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if(touches->containsObject(beginTouch)==false){
        return;
    }
    beginTouch = NULL;
}